package com.vcc.boss.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.vcc.core.annotations.RestPack;
import com.vcc.data.entity.Currency;
import com.vcc.service.boss.request.currency.CurrencyDto;
import com.vcc.service.boss.response.currency.CurrencyVoBoss;
import com.vcc.service.service.CurrencyService;
import com.vcc.service.service.UserService;
import com.vcc.service.service.WalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("currency")
public class CurrencyController {

    @Autowired
    private CurrencyService currencyService;
    @Autowired
    private UserService userService;
    @Autowired
    private WalletService walletService;


    //创建币种-对应用户的基础币种
    @RestPack
    @PostMapping("addCurrency")
    @SaCheckLogin
    @SaCheckPermission(value = "admin:currency:list:create")
    public Boolean addCurrency(
            @RequestBody CurrencyDto currencyDto
    ) {
        List<Long> userIds = userService.listIds();
        Currency currency = currencyService.addCurrency(currencyDto);
        return walletService.init(currency.getId(), currency.getCode(), userIds);
    }

    @RestPack
    @PostMapping("listAll")
    @SaCheckLogin
    @SaCheckPermission(value = "admin:currency:list:view")
    public List<CurrencyVoBoss> listAll() {
        List<Currency> list = currencyService.list();
        return list.stream().map(CurrencyVoBoss::new).collect(Collectors.toList());
    }

}
